package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"jd_mall_users/user_srv/app_config"
	"sync"
)

var (
	db   *gorm.DB
	err  error
	once sync.Once
)

func InitMysql() {
	app := app_config.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", app.User, app.Password, app.Host, app.Port, app.DbName)
	once.Do(func() {
		db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&User{})
	fmt.Println("mysql 动态配置成功:", dsn)
}
